﻿package org.hsl.haxe.translation;
import org.hsl.haxe.CommonSignaler;
import org.hsl.haxe.IDispatcher;
import org.hsl.haxe.translation.ITranslator;
/**
 * The translating signaler is a signaler that dispatches signals, both when the dispatcher calls the dispatch method and when
 * a native event is dispatched. Native events will be translated to signals.
 */
class TranslatingSignaler<D> extends CommonSignaler<D> {
	private var translator:ITranslator<D>;
	/**
	 * Creatos a new translating signaler. The passed translator will be used for the translation of any native events. The
	 * passed dispatcher will be stored as the dispatcher property.
	 */
	public function new(dispatcher:IDispatcher, translator:ITranslator<D>):Void {
		super(dispatcher);
		// If the passed translator is null, store a null object implementation. This will ensure that the translator is never
		/// null, and therefore there will be no null object reference errors when trying to translate a signal.
		if (translator == null) {
			this.translator = new NullTranslator<D>();
		} else {
			this.translator = translator;
		}
	}
	/**
	 * Dispatches a signal based on data inside a native event.
	 */
	public function dispatchNative(nativeEvent:NativeEvent):Void {
		dispatch(translator.translate(nativeEvent));
	}
}